#!/bin/sh

TOPDIR=`pwd`




file_src1=sensor_b/0.001%_wgn_1_1DOF_200Hz_vK_2_FF_0.999_poly_IJ_8_v.dat
file_src2=sensor_c/0.001%_wgn_1_1DOF_200Hz_vK_2_FF_0.999_poly_IJ_8_v.dat
file_merge=merge.dat




check_file(){

 while [ true ] ;do 
  echo "checking  $1 exist?"
 [ -s "$1" ] && break
  done
  

}

#--------main script -----

cp -a /root/palert/src/libmodbus-3.0.5/tests/lobb.txt $file_src1
cp -a /root/palert/src/libmodbus-3.0.5/tests/lobc.txt $file_src2

 #run octave to generate output data for b
 #   cd sensor_b; octave Recursive_0528.m
    cd $TOPDIR
    check_file $file_src1
    tail -n 5 $file_src1 >  $file_merge
#----------------------------------
    #run octave to generate output data for c
#   cd sensor_b; octave Recursive_0528.m
    cd $TOPDIR
    check_file $file_src2
    tail -n 5 $file_src2 >> $file_merge

  
   echo "Octave fiinish! Continue to work"
   echo "get the max value from 10 recrods from merge.dat"
    num_record=`cat merge.dat | wc -l`

    [ $num_record -eq 10 ] ||  { echo " Error! less than 10 records!"; exit; }
    val=`sort -nr $file_merge -k2 | head -n 1 | cut -f5 -d' '`
 
   echo "Maximum value is $val"

   echo $val > result.dat

   ./outled

 # if [ "$(echo 2.8 '>' 3.9 | bc -l)" -eq 1 ] ; then
 #  echo "true"  
 #else 
 #   echo "false"
 #fi


# float point calculation ....Mmmmmmm....
#    if [ $val -gt 2 ]

# calculate first for b
 
# octive Recursive_0528.m



